<?php

/**
 * ClassName:发帖列表处理器
 * @auditor LeiJunXiang
 * @time 2024/11/06
 */

namespace app\tool\inviteList;

use app\factory\Factory;
use app\modelOpreate\invite\BrowsingHistoryOpe;
use app\modelOpreate\invite\Invite;
use app\modelOpreate\user\ForumUserOpe;
use app\tool\ToolBase;
use think\facade\Cache;

class InviteListHandle extends ToolBase
{
    const TOOL_INVITE_LIST_HANDLE = "InviteListHandle";
    //浏览帖子对象key队列
    protected $viewObjKeyList;
    //推荐帖子队列
    protected $recommendInviteList;
    //用户id
    protected $userId;
    //redis操作对象
    protected $redis;


    /**
     * (推荐帖子队列)开始处理(用户id)
     */
    public function startHandle($userId)
    {
        if ($userId) {
            //初始化数据
            $this->initData($userId);
            //获取已浏览帖子对象key队列
            $this->getAlreadyInviteObjKeyList();
            //获取热度最高帖子队列
            $this->getHotHighInviteList();
            //增加浏览帖子对象key队列
            $this->addViewObjKeyList();
            //获取最新帖子队列
            $this->getNewInviteList();
            //保存已浏览帖子对象key队列
            $this->saveInviteObjKeyList();
            //设置是否关注并转化图片链接队列为数组
            $this->setIsAttention();
            //清除数据
            $this->cleanData();
        } else {
            $this->errCode = FAIL;
            $this->errMsg = "找不到用户，请重试!";
        }
        return $this->recommendInviteList;
    }

    /**
     * 获取个人发帖列表(用户id)
     */
    public function getPersonPostList($userId)
    {
        $toolObj = Factory::getInstance()->getToolObj(Invite::TOOL_INVITE_OPE);
        //查询个人帖子
        $data = $toolObj->selectPersonInvite($userId);

        return $data;
    }

    /**
     * 初始化数据
     */
    protected function initData($userId)
    {
        $this->init();
        $this->userId = $userId;
        //浏览帖子对象key队列
        $this->viewObjKeyList = [];
        //推荐帖子队列
        $this->recommendInviteList = [];
        $this->redis = Cache::store('redis');
    }

    /**
     * 获取已浏览帖子对象key队列
     */
    protected function getAlreadyInviteObjKeyList()
    {
        $list = $this->redis->get($this->userId);
        if ($list) {
            //浏览帖子对象key对列
            $this->viewObjKeyList = $list;
        }
    }


    /**
     * 获取热度最高帖子队列
     */
    protected function getHotHighInviteList()
    {
        $toolObj = Factory::getInstance()->getToolObj(Invite::TOOL_INVITE_OPE);
        //(帖子队列)查询已发布的最热帖子(查询个数，需剔除的帖子对象key队列)
        $this->recommendInviteList = $toolObj->selectHotInvite(5, $this->viewObjKeyList);

    }

    /**
     * 增加浏览帖子对象key队列
     */
    protected function addViewObjKeyList()
    {
        if ($this->recommendInviteList) {
            foreach ($this->recommendInviteList as $invite) {
                $this->viewObjKeyList[] = $invite['objKey'];
            }
        }

    }

    /**
     * 获取最新帖子队列
     */
    protected function getNewInviteList()
    {
        $toolObj = Factory::getInstance()->getToolObj(Invite::TOOL_INVITE_OPE);
        //(帖子数组队列)查询已发布的最新帖子(查询个数,需剔除的帖子对象key队列)
        $arr = $toolObj->selectNewInvite(5, $this->viewObjKeyList);
        $this->recommendInviteList = array_merge($this->recommendInviteList, $arr);
        if (empty($this->recommendInviteList)) {
            $this->errMsg = "暂无帖子，快来发一封吧！";
            $this->errCode = FAIL;
        }

    }

    /**
     * 保存已浏览帖子对象key队列
     */
    protected function saveInviteObjKeyList()
    {
        if (!$this->isGoOn()) {
            return;
        }
        $arr = [];
        foreach ($this->recommendInviteList as $invite) {
            $arr[] = $invite['objKey'];
        }
        $this->redis->set($this->userId, $arr, 60 * 10);

    }


    /**
     * 设置是否关注并转化图片链接队列为数组
     */
    protected function setIsAttention()
    {
        if (!$this->isGoOn()) {
            return;
        }
        $toolObj = Factory::getInstance()->getToolObj(ForumUserOpe::TOOL_FORUM_USER_OPE);
        //获取关注用户id列表(用户id)
        $attentionUserIdList = $toolObj->getAttentionUserIdList($this->userId);
        foreach ($this->recommendInviteList as &$invite) {
            //转换图片链接为数组
            $invite['pictureLinkList'] = json_decode($invite['pictureLinkList'], true);
            //发贴人id
            if ($attentionUserIdList && in_array($invite['publishId'], $attentionUserIdList)) {
                //是否关注
                $invite['isAttention'] = true;
            } else {
                $invite['isAttention'] = false;
            }
        }
    }


    /**
     * 清除数据
     */
    protected function cleanData()
    {
        $this->userId = "";
        //浏览帖子对象key队列
        $this->viewObjKeyList = [];
    }


}